package protocol.JT808_2011;

import java.util.HashMap;

public class Handle808_2011JT01 implements IHandle808_2011JT {

    @Override
    public String handle(JT808_2011Cmd cmd) {
        byte subOrder = cmd.subOrder();

        switch (subOrder){
            case 0x00:
                return handle0100(cmd);
            case 0x02:
                return handle0102(cmd);
            case 0x04:
                return handle0104(cmd);
            default:
                return "808-2011:未匹配的命令字"+(cmd.readOrder()+","+cmd.subOrder());
        }
    }

    private String handle0100(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:终端注册");
        builder.append("\r\n");
        builder.append("省域ID："+cmd.readInt(2));
        builder.append("\r\n");
        builder.append("市县域："+cmd.readInt(2));
        builder.append("\r\n");

        builder.append("制造商ID："+cmd.readBCD(5));
        builder.append("\r\n");
        builder.append("终端型号："+cmd.readBCD(8));
        builder.append("\r\n");
        builder.append("终端ID："+cmd.readBCD(7));
        builder.append("\r\n");
        builder.append("车牌颜色："+cmd.readBCD(1));
        builder.append("\r\n");
        builder.append("车牌："+cmd.readString(-1));
        builder.append("\r\n");


        return builder.toString();
    }

    private String handle0104(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:设置终端参数应答");
        builder.append("\r\n");
        builder.append("应答流水号:"+cmd.readInt(2));
        int size = cmd.readInt(1);
        builder.append("参数总数:"+size);

        handleA10(cmd,builder);

        return builder.toString();
    }

    private void handleA10(JT808_2011Cmd cmd, StringBuilder builder){
        do{
            int id = cmd.readInt(4);
            int len = cmd.readInt(1);
            builder.append("    "+Integer.toHexString(id)+"-");
            switch (id) {
                case 0x0001:
                    builder.append("终端心跳发送间隔，秒:" + cmd.readInt(len));
                    break;
                case 0x0002:
                    builder.append("TCP消息应答超时时间，秒:" + cmd.readInt(len));
                    break;
                case 0x0003:
                    builder.append("TCP消息重传次数:" + cmd.readInt(len));
                    break;
                case 0x0004:
                    builder.append("UDP消息应答超时时间，秒:" + cmd.readInt(len));
                    break;
                case 0x0005:
                    builder.append("UDP消息重传次数:" + cmd.readInt(len));
                    break;
                case 0x0006:
                    builder.append("SMS消息应答超时时间，秒:" + cmd.readInt(4));
                    break;
                case 0x0007:
                    builder.append("SMS消息重传次数:" + cmd.readInt(4));
                    break;
                case 0x0010:
                    builder.append("主服务器APN/PPP号码，无线通信拨号访问点:" + cmd.readString(len));
                    break;
                case 0x0011:
                    builder.append("主服务器无线通信拨号用户名:" + cmd.readString(len));
                    break;
                case 0x0012:
                    builder.append("主服务器无线通信拨号密码:" + cmd.readString(len));
                    break;
                case 0x0013:
                    builder.append("主服务器地址,IP或域名:" + cmd.readString(len));
                    break;
                case 0x0014:
                    builder.append("备份服务器APN，无线通信拨号访问点:" + cmd.readString(len));
                    break;
                case 0x0015:
                    builder.append("备份服务器无线通信拨号用户名:" + cmd.readString(len));
                    break;
                case 0x0016:
                    builder.append("备份服务器无线通信拨号密码:" + cmd.readString(len));
                    break;
                case 0x0017:
                    builder.append("备份服务器地址,IP或域名:" + cmd.readString(len));
                    break;
                case 0x0018:
                    builder.append("服务器TCP端口:" + cmd.readInt(len));
                    break;
                case 0x0019:
                    builder.append("服务器UDP端口:" + cmd.readInt(len));
                    break;
                case 0x0020: {
                    HashMap<Integer, String> list = new HashMap<>();
                    int type = cmd.readInt(len);
                    list.put(0, "定时汇报");
                    list.put(1, "定距汇报");
                    list.put(2, "定时 + 定距汇报");
                    builder.append("位置汇报策略:" + list.getOrDefault(type, "未定义"));
                    break;
                }
                case 0x0021: {
                    HashMap<Integer, String> list = new HashMap<>();
                    int type = cmd.readInt(len);
                    list.put(0, "根据ACC状态");
                    list.put(1, "根据登录状态 + ACC状态，先判断登录状态，若登录再根据ACC状态");
                    builder.append("位置汇报方案:" + list.getOrDefault(type, "未定义"));
                    break;
                }
                case 0x0022:
                    builder.append("未登录汇报时间间隔，秒:" + cmd.readInt(len));
                    break;
                case 0x0027:
                    builder.append("休眠时汇报时间间隔，秒:" + cmd.readInt(len));
                    break;
                case 0x0028:
                    builder.append("紧急报警时汇报时间间隔，秒:" + cmd.readInt(len));
                    break;
                case 0x0029:
                    builder.append("缺省时间汇报距离间隔，米:" + cmd.readInt(len));
                    break;
                case 0x002C:
                    builder.append("缺省距离汇报间隔，米:" + cmd.readInt(len));
                    break;
                case 0x002D:
                    builder.append("驾驶员未登录汇报距离间隔，米:" + cmd.readInt(len));
                    break;
                case 0x002E:
                    builder.append("休眠时汇报距离间隔，米:" + cmd.readInt(len));
                    break;
                case 0x002F:
                    builder.append("紧急报警时汇报距离间隔，米:" + cmd.readInt(len));
                    break;
                case 0x0030:
                    builder.append("拐点补传角度:" + cmd.readInt(len));
                    break;
                case 0x0040:
                    builder.append("监控平台电话号码:" + cmd.readString(len));
                    break;
                case 0x0041:
                    builder.append("复位电话号码:" + cmd.readString(len));
                    break;
                case 0x0042:
                    builder.append("恢复出厂设置电话号码:" + cmd.readInt(len));
                    break;
                case 0x0043:
                    builder.append("监控指挥中心SMS电话号码:" + cmd.readInt(len));
                    break;
                case 0x0044:
                    builder.append("接收终端SMS文本报警号码:" + cmd.readInt(len));
                    break;
                case 0x0045: {
                    HashMap<Integer, String> list = new HashMap<>();
                    int type = cmd.readInt(len);
                    list.put(0, "自动接听");
                    list.put(1, "ACC ON时自动接听，OFF时手动接听");
                    builder.append("终端电话接听策略:" + list.getOrDefault(type, "未定义"));
                    break;
                }
                case 0x0046:
                    builder.append("每次最长通话时间，秒:" + cmd.readInt(len));
                    break;
                case 0x0047:
                    builder.append("当月最长通话时间，秒:" + cmd.readInt(len));
                    break;
                case 0x0048:
                    builder.append("监听电话号码:" + cmd.readString(len));
                    break;
                case 0x0049:
                    builder.append("监管平台特权短信号码:" + cmd.readString(len));
                    break;
                case 0x0050:
                    builder.append("报警屏蔽字，与位置信息汇报消息中的报警标志相对应，相应位为1则相应报警被屏蔽:" + cmd.readBCD(len));
                    break;
                case 0x0051:
                    builder.append("报警发送文本SMS开关，与位置信息汇报消息中的报警标志相对应，相应位为1则相应报警时发送文本SMS:" + cmd.readBCD(len));
                    break;
                case 0x0052:
                    builder.append("报警拍摄开关，与位置信息汇报消息中的报警标志相对应，相应位为1则相应报警时摄像头拍摄:" + cmd.readBCD(len));
                    break;
                case 0x0053:
                    builder.append("报警拍摄存储标志，与位置信息汇报消息中的报警标志相对应，相应位为1则对相应报警时拍的照片进行存储，否则实时上传:" + cmd.readBCD(len));
                    break;
                case 0x0054:
                    builder.append("关键标志，与位置信息汇报消息中的报警标志相对应，相应位为1则对相应报警为关键报警:" + cmd.readBCD(len));
                    break;
                case 0x0055:
                    builder.append("最高速度，KM/H:" + cmd.readInt(len));
                    break;
                case 0x0056:
                    builder.append("超速持续时间，秒:" + cmd.readInt(len));
                    break;
                case 0x0057:
                    builder.append("连续驾驶时间门限，秒:" + cmd.readInt(len));
                    break;
                case 0x0058:
                    builder.append("当天累计驾驶时间门限，秒:" + cmd.readInt(len));
                    break;
                case 0x0059:
                    builder.append("最小休息时间，秒:" + cmd.readInt(len));
                    break;
                case 0x005A:
                    builder.append("最长停车时间，秒:" + cmd.readInt(len));
                    break;
                case 0x0070:
                    builder.append("图像/视频质量，1～10，1最好:"+cmd.readInt(len));
                    break;
                case 0x0071:
                    builder.append("亮度:"+cmd.readInt(len));
                    break;
                case 0x0072:
                    builder.append("对比度:"+cmd.readInt(len));
                    break;
                case 0x0073:
                    builder.append("饱和度:"+cmd.readInt(len));
                    break;
                case 0x0074:
                    builder.append("色度:"+cmd.readInt(len));
                    break;
                case 0x0080:
                    builder.append("车辆里程表读数，1/10KM:"+cmd.readInt(len));
                    break;
                case 0x0081:
                    builder.append("车辆所在的省域ID，1～255（省级行政区划代码前2位；终端根据此ID生成所在省的简称即对应车牌号第一个汉字）:"+cmd.readInt(len));
                    break;
                case 0x0082:
                    builder.append("车辆所在的市域ID，1～255（行政区划代码的第3、4位）:"+cmd.readInt(len));
                    break;
                case 0x0083:
                    builder.append("公安交通管理部分颁发的机动车牌:"+cmd.readString(len));
                    break;
                case 0x0084:
                    builder.append("车牌颜色:"+cmd.readInt(len));
                    break;
                default:
                    builder.append("未定义ID("+id+"):"+cmd.readBCD(len));
                    break;
            }

            builder.append("\r\n");
        }while (cmd.leftSize() > 3);
    }

    private String handle0102(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:终端鉴权");
        builder.append("\r\n");

        builder.append("鉴权码："+cmd.readString(cmd.leftSize()));
        builder.append("\r\n");

        return builder.toString();
    }
}
